package org.kailu.book.controller;

import org.kailu.book.entity.TypeEntity;
import org.kailu.book.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author viet.ch
 * @generated May 25, 2014
 * @from Ho Chi Minh, VN
 */

@Controller
public class TypeController {

	@Autowired
	private TypeService typeService;
	
	@RequestMapping("type")
	public String getData(ModelMap map) {
		map.addAttribute("typeList", typeService.getAll());
		return "type";
	}
	
	@RequestMapping(value = "/type/saveorupdate", method = RequestMethod.GET)
	public String initData(ModelMap map) {
		map.addAttribute("type",new TypeEntity());
		return "type_new";
	}
	
	@RequestMapping(value = "/type/saveorupdate", method = RequestMethod.POST)
    public String add(@ModelAttribute(value="type") TypeEntity typeEntity, BindingResult result, ModelMap map)
    {
		typeService.saveOrUpdate(typeEntity);
		return "redirect:/type";
    }
	
	@RequestMapping(value = "/type/saveorupdate/{typeId}", method = RequestMethod.GET)
    public String update(@PathVariable("typeId") int typeId, ModelMap map)
    {
		map.addAttribute("type", typeService.get(typeId));
		return "type_new";
    }
	
	@RequestMapping(value = "/type/delete/{typeId}", method = RequestMethod.GET)
    public String delete(@PathVariable("typeId") int typeId)
    {
		this.typeService.delete(typeId);
		return "redirect:/type";
    }
}
